USE [ATAChietkhau]
GO

/****** Object:  StoredProcedure [dbo].[KhachHang_GetPaged]    Script Date: 07/26/2010 22:38:52 ******/
SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER ON
GO


/*
----------------------------------------------------------------------------------------------------

-- Created By: ATA ()
-- Purpose: Gets records from the KhachHang table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


ALTER PROCEDURE [dbo].[KhachHangLapMayPhaMau_GetListByCondition]
(

	@WhereClause varchar (2000)
)
AS


				
				BEGIN
				
				-- SQL Server 2005 Paging
				DECLARE @SQL AS nvarchar(MAX)
				SET @SQL = 'SELECT * FROM ('
				SET @SQL = @SQL + ' SELECT khdt.ID, khdt.KhachHangID, cus.MaKH, cus.TenKH AS TenHoiVien, khdt.LoaiMay'
				SET @SQL = @SQL + ' khdt.SoBienBan, khdt.NgayKyBienBan, khdt.ThoiDiemLapMay as StartDate'
				SET @SQL = @SQL + ' FROM KhachHangLapMayPhaMau khdt'
				SET @SQL = @SQL + ' JOIN vwAllCustomers cus ON cus.KhachHangID = khdt.KhachHangID) A'
				
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				
				EXEC sp_executesql @SQL
			
				END

--SELECT * FROM (			
--SELECT khdt.*, th.BrandCode, th.BrandName, cus.MaKH, cus.TenKH, ct.TenChuongTrinh
--FROM KhachHangDuthuong khdt
--JOIN ThuongHieu th ON khdt.BrandID = th.BrandID
--JOIN vwAllCustomers cus ON cus.KhachHangID = khdt.HoiVienID
--JOIN dbo.ChuongTrinh ct ON ct.ChuongTrinhID = khdt.ChuongTrinhID) A
--WHERE 
--1 = 1 AND SoBienBan LIKE '%%' AND BrandID = 1 AND ChuongTrinhID = 1 AND Duration = 36 AND StartDate >= CONVERT(datetime, '06/01/2010', 101)

GO


EXEC KhachHangLapMayPhaMau_GetListByCondition ''